home *** CD-ROM | disk | FTP | other *** search
- PAGE 60,132
- TITLE Conversion routines
- SUBTTL Called by file: dosknows.asm
-
-
- ;-----------------------------------------------------------------------;
- ; Convers.asm June 1, 1985 Ver 1.0 ;
- ; ;
- ; this file contains the support routines to dosknows.asm ;
- ; ;
- ; Anil Bharvaney ;
- ; @fido 79 ;
- ; (212) 535-8924 ;
- ;-----------------------------------------------------------------------;
-
-
- SUBTTL All public routines declared here
- PAGE
-
-
-
- ;------------------------------------------------------------;
- ; All public routines are named over here ;
- ;------------------------------------------------------------;
-
- PUBLIC Stdin,Stdout,Strout,ToHEX,ToDec,ToDecW
-
-
- SUBTTL all major equates
- PAGE
-
-
-
- ; all major equates are here
-
- DOS EQU 21H
- INCHAR EQU 01H
- OUTCHAR EQU 02H
- OUTSTRING EQU 09H
- TERMINATOR EQU '$'
- CR EQU 13
- LF EQU 10
-
-
- SUBTTL Data Segment
- PAGE
-
-
-
- DSEG SEGMENT PUBLIC PARA 'DATA'
-
- ASCII DB '0123456789ABCDEF'
-
- DSEG ENDS
-
-
-
- SUBTTL code segment
- PAGE
-
-
- CSEG SEGMENT PUBLIC PARA 'CODE'
-
- ASSUME CS:CSEG,DS:DSEG
-
- SUBTTL PROCEDURE STDIN
- PAGE
-
-
- ;------------------------------------------------------------------------------;
- ; PROCEDURE: Stdin ;
- ; Accepts input of one character. Echos and waits. ;
- ; In: ;
- ; Nothing ;
- ; Out: ;
- ; Char received is in register AL ;
- ;------------------------------------------------------------------------------;
-
- STDIN PROC NEAR
-
- MOV AH,INCHAR ;input w/ echo and wait of one char
- INT DOS ;DOS call
- RET
-
- STDIN ENDP
-
-
-
- SUBTTL PROCEDURE STDOUT
- PAGE
-
-
- ;------------------------------------------------------------------------------;
- ; PROCEDURE: Stdout ;
- ; Prints out one character to the console. ;
- ; In: ;
- ; AL character to print ;
- ; Out: ;
- ; Nothing. ;
- ;------------------------------------------------------------------------------;
-
- STDOUT PROC NEAR
-
- PUSH DX ;save register
- PUSH AX
-
- MOV DL,AL ;mov character
- MOV AH,OUTCHAR ;output character in AL to Console
- INT DOS ;Dos call
-
- POP AX ;restore user register
- POP DX ;restore user register
- RET
-
- STDOUT ENDP
-
-
- SUBTTL PROCEDURE STROUT
- PAGE
-
-
- ;------------------------------------------------------------------------------;
- ; PROCEDURE: Strout ;
- ; Prints out one string terminated by $. ;
- ; In: ;
- ; DS:DX points to the string to be printed to the console ;
- ; Out: ;
- ; Nothing. ;
- ;------------------------------------------------------------------------------;
-
- STROUT PROC NEAR
-
- PUSH DX ;save caller's registers
- PUSH AX
-
- MOV AH,Outstring ;func to write $ terminated string
- INT DOS
-
- POP AX ;restore caller registers
- POP DX
- RET
-
- STROUT ENDP
-
-
-
-
-
-
- SUBTTL PROCEDURE TOHEX
- PAGE
-
-
-
- ;------------------------------------------------------------------------------;
- ; PROCEDURE: TOHEX ;
- ; Converts Word to Ascii Hexadecimal and displays it on the screen;
- ; In: ;
- ; DX has binary number to be converted ;
- ; Out: ;
- ; Nothing. Output goes to console ;
- ;------------------------------------------------------------------------------;
-
- TOHEX PROC NEAR
-
- PUSH AX ;save registers used
- PUSH BX
- PUSH CX
-
- LEA BX,ASCII ;translate table address
- MOV CX,4 ;four nibbles to a word, (need 4 hex chars)
- More:
- PUSH CX ;save this
- MOV CL,4 ;move nibble msb to lsb
- ROL DX,CL ;moves upper nibble to lsb's lower nibble
- MOV AL,DL ;move rotated in nibble to al
- AND AL,0FH ;don't need the upper nibble
- XLAT ASCII ;translate it to Hex
- CALL Stdout ;prints char in AL
- POP CX ;restore the count
- LOOP More ;do the other nibbles..
-
- POP CX ;restore user registers
- POP BX
- POP AX
- RET
-
- TOHEX ENDP
-
-
-
-
- SUBTTL PROCEDURE TO DECIMAL (BYTE)
- PAGE
-
-
- ;------------------------------------------------------------------------------;
- ; PROCEDURE: TODEC ;
- ; Converts BYTE to Ascii Decimal and displays it on the screen;
- ; In: ;
- ; DL has binary number to be converted ;
- ; Out: ;
- ; Nothing. Output goes to console ;
- ;------------------------------------------------------------------------------;
-
- TODEC PROC NEAR
-
- PUSH AX ;Save registers used in routine
- PUSH CX
-
- XOR AH,AH
- MOV AL,DL ;Get the byte to be converted
- XOR CL,CL ;Counts # of words stuffed onto stack
- LP:
- AAM ;AL/10 remainder in AL, quotient in AH
- ADD AL,'0' ;make it ascii
- MOV CH,AH ;save it for later
- XOR AH,AH ;getting ready for pushing onto stack
- PUSH AX ;save it onto stack
- MOV AL,CH ;restore the number
- INC CL ;increment count of words on stack
- CMP AX,0 ;Are we done?
- JE PRTIT ; if done, print it out
- JMP LP ;continue conversion
-
- PRTIT: ;done converting
- CMP CL,0 ;anything to left to pop up and print?
- JE DONE ; if nothing left, bye
- POP AX ;pop up the number
- DEC CL ;decrease count of words on stack
- CALL Stdout ;print the ascii character in AL
- JMP Prtit ;while more characters to print, print
-
- DONE:
- POP CX ;restore callers registers
- POP AX
- RET
-
-
- TODEC ENDP
-
-
- SUBTTL PROCEDURE TO DECIMAL (WORD)
- PAGE
-
-
- ;------------------------------------------------------------------------------;
- ; PROCEDURE: TODECW ;
- ; Converts WORD to Ascii Decimal and displays it on the screen;
- ; In: ;
- ; DX has binary number to be converted ;
- ; Out: ;
- ; Nothing. Output goes to console ;
- ;------------------------------------------------------------------------------;
-
- TODECW PROC NEAR
-
- PUSH AX ;save caller's registers
- PUSH BX
- PUSH CX
- PUSH DX
-
- MOV AX,DX ;get ready to divide dx:ax
- MOV BX,10 ;divide by 10
- XOR CX,CX ;Initialize counter
-
- LPW:
- XOR DX,DX ;top word is zero
- DIV BX ;remainder is in dx, ax has quotient
- ADD DL,'0' ;adjust remainder to be ascii
- PUSH DX ;save it, last one to be printed
- INC CX ;increment counter
- CMP AX,09 ;are we done yet
- JA LPW ; if not done, repeat it again
-
- ADD AL,'0' ;the most significant character is here
- CALL Stdout
-
-
- WPRTIT: ;done converting
- CMP CX,0 ;anything to left to pop up and print?
- JE WDONE ; if nothing left, bye
- POP AX ;pop up the number
- DEC CX ;decrease count of words on stack
- CALL Stdout ;print the ascii character in AL
- JMP WPrtit ;while more characters to print, print
-
- WDONE:
- POP DX ;restore caller's registers
- POP CX
- POP BX
- POP AX
-
- RET
-
- TODECW ENDP
-
-
- CSEG ENDS
- END